package com.fc.mall.portal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fc.mall.mapper.OmsOrderMapper;
import com.fc.mall.mapper.UmsMemberAccountChangeHistoryMapper;
import com.fc.mall.mapper.UmsMemberInviteHistoryMapper;
import com.fc.mall.model.*;
import com.fc.mall.portal.dao.PortalMemberDao;
import com.fc.mall.portal.dao.PortalOrderDao;
import com.fc.mall.portal.service.UmsMemberAccountService;
import com.fc.mall.portal.service.UmsMemberService;
import com.fc.mall.portal.service.UmsMemberStatisticeService;
import com.fc.mall.util.Constants;
import com.fc.mall.util.DateTimeHelper;
import com.fc.mall.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Sunny on 2019/7/2.
 */
@Service
public class UmsMemberStatisticeServiceImpl implements UmsMemberStatisticeService {

    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    private UmsMemberAccountService memberAccountService;

    @Autowired
    private UmsMemberInviteHistoryMapper memberInviteHistoryMapper;

    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Autowired
    private UmsMemberAccountChangeHistoryMapper accountChangeHistoryMapper;

    @Autowired
    private PortalOrderDao portalOrderDao;

    @Autowired
    private PortalMemberDao portalMemberDao;
    /**
     * 统计代理商基本信息
     *  累计收入
     *  粉丝总量
     *  粉丝订单总量
     *
     *  今日入账
     *  今日预估
     *  待结算
     * @return
     */
    @Override
    public JSONObject getMemberAgentData() {

        JSONObject respJson = new JSONObject();
        UmsMemberAccount memberAccount = memberAccountService.getMemberAccountInfo();

        respJson.put("totalAmount", memberAccount.getTotalAmount());
        respJson.put("waitAmount", memberAccount.getWaitAmount());

        UmsMemberInviteHistoryExample inviteHistoryExample = new UmsMemberInviteHistoryExample();
        inviteHistoryExample.createCriteria().andMemberIdEqualTo(memberAccount.getMemberId());
        List<UmsMemberInviteHistory> inviteHistoryList = memberInviteHistoryMapper.selectByExample(inviteHistoryExample);

        respJson.put("totalFanCount", inviteHistoryList.size());

        UmsMemberAccountChangeHistoryExample changeHistoryExample = new UmsMemberAccountChangeHistoryExample();
        changeHistoryExample.createCriteria().andMemberIdEqualTo(memberAccount.getMemberId());
        List<UmsMemberAccountChangeHistory> changeHistoryList = accountChangeHistoryMapper.selectByExample(changeHistoryExample);
        List<String> orderSnList = new ArrayList<>();
        if(!changeHistoryList.isEmpty())
        {
            for (UmsMemberAccountChangeHistory history: changeHistoryList)
            {
                orderSnList.add(history.getOrderSn());
            }
        }else
        {
            orderSnList.add("0");
        }
        OmsOrderExample orderExample = new OmsOrderExample();
        orderExample.createCriteria().andStatusBetween(1,3).andOrderSnIn(orderSnList);

        int totalOrderCount = omsOrderMapper.countByExample(orderExample);
        respJson.put("totalOrderCount", totalOrderCount);

        UmsMemberAccountChangeHistoryExample historyExample = new UmsMemberAccountChangeHistoryExample();
        historyExample.createCriteria().andMemberIdEqualTo(memberAccount.getMemberId()).andCreateTimeBetween(DateTimeHelper.getCurrentDateFromZero(), new Date());

        List<UmsMemberAccountChangeHistory> accountChangeHistoryList = accountChangeHistoryMapper.selectByExample(historyExample);
        Long todayAbleAmount = 0L;
        Long todayWaitAmount = 0L;
        for(UmsMemberAccountChangeHistory history: accountChangeHistoryList)
        {
            if(history.getAccountType().equals(Constants.AccountType.WaitAccount.getValue()))
            {
                todayWaitAmount += history.getAmount();
            }
            else if(history.getAccountType().equals(Constants.AccountType.MemberAccount.getValue()))
            {
                todayAbleAmount += history.getAmount();
            }
        }
        respJson.put("todayWaitAmount", todayWaitAmount);
        respJson.put("todayAbleAmount", todayAbleAmount);
        return respJson;
    }

    @Override
    public JSONObject getMemberIncomeData(Integer filterType) {
        JSONObject incomeJson = new JSONObject();
        UmsMemberAccount memberAccount = memberAccountService.getMemberAccountInfo();
        BigDecimal  totalPaymentAmount = portalOrderDao.getTotalPayAmountForFans(memberAccount.getMemberId());
        incomeJson.put("totalPaymentAmount", totalPaymentAmount);
        incomeJson.put("totalAmount", new BigDecimal(memberAccount.getTotalAmount()).divide(new BigDecimal(100)));

        List<UmsMemberAccountChangeHistory> accountChangeHistoryList = portalMemberDao.getAccountHistory(memberAccount.getMemberId(), filterType);
        Integer paymentCount = 0;
        Long totalWaitAmount = 0L;
        Long totalAbleAmount = 0L;

        /**
         * 记录是否有重复订单号。
         * 如果重复则不累加订单数量
         */
        List<String>paymentOrderSn = new ArrayList();
        for(UmsMemberAccountChangeHistory history : accountChangeHistoryList)
        {
            if(history.getAccountType().equals(Constants.AccountType.WaitAccount.getValue()))
            {
                totalWaitAmount += history.getAmount();
                if(paymentOrderSn.contains(history.getOrderSn()))
                {
                    continue;
                }
                paymentCount ++;
                paymentOrderSn.add(history.getOrderSn());
            }
            else
            {
                totalAbleAmount += history.getAmount();
            }
        }
        incomeJson.put("paymentCount", paymentCount);
        incomeJson.put("totalWaitAmount", totalWaitAmount);
        incomeJson.put("totalAbleAmount", totalAbleAmount);
        return incomeJson;
    }
}
